package org.example;

/**
 * @Author: tang
 * @Description:
 * @Date 2024/5/6 10:22
 */
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.util.IOUtil;
import com.wechat.pay.java.service.billdownload.BillDownloadService;
import com.wechat.pay.java.service.billdownload.BillDownloadServiceExtension;
import com.wechat.pay.java.service.billdownload.DigestBillEntity;
import com.wechat.pay.java.service.billdownload.model.*;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.StringReader;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.stream.Collectors;

/** BillDownloadService使用示例 */
public class BillDownloadServiceExample {

    /** 商户号 */
    public static String merchantId = "1593191541";
    /** 商户API私钥路径 */
    public static String privateKeyPath = "/Users/tanglonglong/idea_project/cloud-test/web-springboot/web-wx/src/main/resources/wx/apiclient_key.pem";
    /** 商户证书序列号 */
    public static String merchantSerialNumber = "73AAF1F8897D4ECE9C6A468D2621410D2BADAF2A";
    /** 商户APIV3密钥 */
    public static String apiV3Key = "77be67fe81c437f7d95e2bebd2082cb5";

    public static BillDownloadService service;

    public static BillDownloadServiceExtension billDownloadServiceExtension;

    public static void main(String[] args) throws Exception{
        // 初始化商户配置
        Config config =
                new RSAAutoCertificateConfig.Builder()
                        .merchantId(merchantId)
                        // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
                        .privateKeyFromPath(privateKeyPath)
                        .merchantSerialNumber(merchantSerialNumber)
                        .apiV3Key(apiV3Key)
                        .build();


        // 初始化服务
        service = new BillDownloadService.Builder().config(config).build();

        billDownloadServiceExtension = new BillDownloadServiceExtension.Builder().config(config).build();

        QueryBillEntity tradeBill = getTradeBill();
        System.out.println(tradeBill);

        String date = "2024-05-21";
        // 柴米商馆
        String subMchid = "1615555941";
        // 交易账单
        DigestBillEntity digestBillEntity = getTradeBillInputStream(date,subMchid);
        InputStream inputStream = digestBillEntity.getInputStream();

//        BufferedReader bufferedReader = new BufferedReader(new StringReader(IOUtil.toString(inputStream)));
//        List<String> billListSource = bufferedReader.lines().collect(Collectors.toList());
//        List<String> billList = billListSource.subList(1, billListSource.size() - 2);
//
//        for (String bill : billList) {
//            String replaceBill = bill.replace("`", "");
//            System.out.println(replaceBill);
//            String[] billRow = replaceBill.split(",");
//        }

        Files.copy(inputStream, Paths.get("/Users/tanglonglong/Downloads",date+"-all.csv"), StandardCopyOption.REPLACE_EXISTING);
    }
    /** 申请资金账单API */
    public static QueryBillEntity getFundFlowBill() {

        GetFundFlowBillRequest request = new GetFundFlowBillRequest();
        return service.getFundFlowBill(request);
    }
    /** 申请单个子商户资金账单API */
    public static QueryEncryptBillEntity getSingleSubMchFundFlowBill() {
        GetSingleSubMchFundFlowBillRequest request = new GetSingleSubMchFundFlowBillRequest();
        return service.getSingleSubMchFundFlowBill(request);
    }
    /** 申请二级商户资金账单API */
    public static QueryEncryptBillEntity getAllSubMchFundFlowBill() {

        GetAllSubMchFundFlowBillRequest request = new GetAllSubMchFundFlowBillRequest();
        return service.getAllSubMchFundFlowBill(request);
    }

    /** 申请交易账单API */
    public static QueryBillEntity getTradeBill() {
        GetTradeBillRequest request = new GetTradeBillRequest();
        request.setBillDate("2024-05-04");
        request.setSubMchid("1602410343");
        return service.getTradeBill(request);
    }

    /**
     * 交易账单 文件流
     * @param date
     * @return
     */
    public static DigestBillEntity getTradeBillInputStream(String date,String subMchid) {
        GetTradeBillRequest request = new GetTradeBillRequest();
        request.setBillType(BillType.ALL);
        request.setBillDate(date);
        request.setSubMchid(subMchid);
//        request.setSubMchid("1602410343");
        return billDownloadServiceExtension.getTradeBill(request);
    }

    public static DigestBillEntity getFundFlowInputStream(String date) {
        GetFundFlowBillRequest request = new GetFundFlowBillRequest();
//        request.setBillType(BillType.SUCCESS);
        request.setBillDate(date);
        return billDownloadServiceExtension.getFundFlowBill(request);
    }



}
